package com.haoshuang.zhouzhoubang.services

import android.Manifest
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.location.Location
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.util.Log
import androidx.core.app.NotificationCompat
import com.blankj.utilcode.util.PermissionUtils
import com.haoshuang.zhouzhoubang.MyApp
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.model.api.ApiRepo
import com.haoshuang.zhouzhoubang.ui.page.splash.SplashPageActivity
import com.haoshuang.zhouzhoubang.utils.LocationUtils
import com.lnkj.jialubao.utils.AccountUtils
import com.mufeng.libs.MFLibs


class MyWorkService : Service() {

    private var notificationManager: NotificationManager? = null

    override fun onBind(p0: Intent?): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()
        notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager?
        createNotification()
    }

    override fun onDestroy() {
        super.onDestroy()
        stopForeground(STOP_FOREGROUND_REMOVE)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        requestLoginStatus()
        return START_STICKY
    }

    private fun requestLoginStatus() {
        if (AccountUtils.isLogin) {
            ApiRepo.getLoginStatus()
        }
        MFLibs.handler.postDelayed({
            requestLoginStatus()
        }, 50000)
    }

    private fun createNotification() {
        val CHANNEL_ONE_ID = "1001"
        //创建点击跳转Activity
        val intent = Intent(this, SplashPageActivity::class.java)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
        val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
        //创建notification
        val builder = NotificationCompat.Builder(applicationContext, CHANNEL_ONE_ID)
            .setContentIntent(pendingIntent) // 设置PendingIntent
            .setSmallIcon(R.mipmap.logo) // 设置状态栏内的小图标
            //.setLargeIcon(bitmapIcon)// 设置大图标
            .setContentTitle("周周帮")
            .setContentText("点击打开应用") // 设置内容
            .setWhen(System.currentTimeMillis())// 设置该通知发生的时间
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)// 锁屏显示全部通知
            //.setDefaults(Notification.DEFAULT_ALL)// //使用默认的声音、振动、闪光
            .setCategory(Notification.CATEGORY_SERVICE)//设置类别
            .setPriority(NotificationCompat.PRIORITY_MAX)// 优先级为：重要通知

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //安卓8.0以上系统要求通知设置Channel,否则会报错
            val notificationChannel = NotificationChannel(
                CHANNEL_ONE_ID,
                "服务常驻通知",
                NotificationManager.IMPORTANCE_HIGH
            )
            notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC//锁屏显示全部通知
            notificationManager?.createNotificationChannel(notificationChannel)
            builder.setChannelId(CHANNEL_ONE_ID)
        }
        val notification = builder.build() // 获取构建好的Notification
        //notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
        notification.flags = Notification.FLAG_ONGOING_EVENT;//不消失的常驻通知
        startForeground(1, notification);//设置常驻通知
    }

}